package com.chun.service.impl;

import com.chun.annotation.MyAnno;
import com.chun.mapper.EmpMapper;
import com.chun.pojo.Emp;
import com.chun.pojo.GenderCountDTO;
import com.chun.pojo.JobDataCountDTO;
import com.chun.pojo.PageBean;
import com.chun.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean list(Integer pageNo, Integer pageSize) {
//        //方法一
//        Long count = empMapper.count();
//
//        Integer startIndex = (pageNo - 1) * pageSize;
//        List<Emp> empList = empMapper.list(startIndex, pageSize);
//
//        PageBean pageBean = new PageBean(count, empList);
//        return pageBean;
        //方法二
        //使用插件
        PageHelper.startPage(pageNo, pageSize);
        //调用mapper
        List<Emp> empList = empMapper.list();
        //强制类型转换
        Page<Emp> page = (Page<Emp>) empList;
        //封装晨报PageBean
        PageBean pageBean = new PageBean(page.getTotal(), page.getResult());
        return pageBean;
    }

    @Override
    public PageBean list2(String name, Short gender, Short job,LocalDate begin, LocalDate end,
                          Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);

        //调用mapper
        List<Emp> empList = empMapper.list2(name,gender, job, begin, end);
        //强制类型转换
        Page<Emp> page = (Page<Emp>) empList;
        //封装PageBean
        PageBean pageBean = new PageBean(page.getTotal(), page.getResult());
        return pageBean;
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        empMapper.deleteByIds(ids);
    }

    @Override
    public void add(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.add(emp);
    }
    @MyAnno
    @Override
    public Emp findById(Integer id) {
        Emp emp = empMapper.findById(id);
        return emp;
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp selectByName(Emp emp) {
        Emp emp2 = empMapper.selectByName(emp);
        return emp2;
    }

    @Override
    public List<GenderCountDTO> getEmpGenderData() {
        List<GenderCountDTO> genderCountDTOList = empMapper.getEmpGenderData();
        return genderCountDTOList;
    }

    @Override
    public JobDataCountDTO getEmpJobData() {
        List<Integer> dataList = empMapper.getDataList();
        List<String> jobList = empMapper.getJobList();
        JobDataCountDTO jobDataCountDTO = new JobDataCountDTO(jobList, dataList);
        return jobDataCountDTO;
    }
}
